$(function () {
    $("#page").load("/html/admin/page.html");
    // findAll();
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        userName: $("#userName").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        hobby: getHobby().toLocaleString(),
        sex: $("#sex").val(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        status: $('input:radio[name="status"]:checked').val(),
        curPage: $('#curPage').val(),
        pageSize: $('#pageSize').val()

    };
    let resultList = myAjax("/admin/user/findAll",data,"post");
    console.log(resultList);
    let  result = adminLogin(resultList);
    //总条数
    if (result) {
        $("#totalCount").html(resultList.count);
        setData(resultList.list);
    }

}

function getHobby() {
    let hobby = [];
    $("input:checkbox[name=hobby]").each(function () {
        if ($(this).prop('checked')) {
            hobby.push($(this).val());
        }
    })
    return hobby;
}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        //id ,userName对应后台UserModel层中的变量
        html += "<tr>\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td>" + clearNull(data[i].userName) + "</td>\n" +
            "            <td>" + clearNull(data[i].phone) + "</td>\n" +
            "            <td>" + clearNull(data[i].email) + "</td>\n" +
            "            <td>" + clearNull(data[i].age) + "</td>\n" +
            "            <td>" + clearNull(data[i].sex) + "</td>\n" +
            "            <td>" + clearNull(data[i].birthday) + "</td>\n" +
            "            <td>" + clearNull(data[i].hobby) + "</td>\n" +
            "            <td>" + (clearNull(data[i].status) === 0 ? "<lable class='normal'>已激活<span class='glyphicon glyphicon-ok'></span></lable>" : '<label class="abnormal">未激活</lable><span class="glyphicon glyphicon-remove" ></span>') + "</td>\n" +
            "            <td>" + clearNull(data[i].createTime) + "</td>\n" +
            "            <td class=\"edit\">\n" +
            "                <div onclick='toEditPage(" + data[i].id + ")'><span class='glyphicon glyphicon-edit' title='编辑'></span></div>\n" +
            "                <div onclick='del(" + data[i].id + ")'><span class='glyphicon glyphicon-trash' title='删除'></span></div>\n" +
            "                <div onclick='editPassword(" + data[i].id + ")'><span class='glyphicon glyphicon-pencil' title='修改密码' ></span></div>\n" +
            "            </td>\n" +
            "        </tr>";
    }
    $("#tbody").html(html);
}


/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}

/***
 * 删除功能
 */
function del(id) {
    let result = myAjax("/admin/user/del", {id: id},"post");
    console.log(id);
    console.log(result);
    if (result.result == 1) {
        //删除成功，就重新查询数据
        alert("删除成功(== ^^ ==)!");
        findAll();
    } else {
        alert("删除失败(╥╯^╰╥)！")
    }

}

/**
 * 编辑操作
 */
function toEditPage(id) {
    //1.拿到当前用户的id，然后将id传到编辑页面
    // 2.编辑页面加载完成后，需要通过id去数据库查询数据，
    // 3.将查询结果，显示在页面上
    //4.sessionStorage 项目开始时，只能时同一浏览器
    //5.localStorage 项目开始时，可以是不同浏览器
    sessionStorage.setItem("userId", id);
    window.load("/user/userEdit.html");
}

/**
 * 修改密码
 * @param id
 */
function editPassword(id) {
    //1.拿到当前用户的id，然后将id传到编辑页面
    // 2.编辑页面加载完成后，需要通过id去数据库查询数据，
    // 3.将查询结果，显示在页面上
    sessionStorage.setItem("userId", id);
    window.load("/user/userEditPassword.html");

}



